// pages/component/auth.js
const app = getApp()
const wh = require('../../utils/wh.js');
Component({
  /**
   * 组件的初始数据
   */
  data: {
    // parent_id: 0,
    auth_modal: false, // 授权窗口
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 是否有上级
    hasParent: function(pid) {
      this.setData({
        parent_id: pid
      })
    },
    // 授权判断
    onCheck: function() {
      let that = this;
      that.setData({
        auth_modal: false
      })
      wx.getExtConfig({
        success: function (res) {
          module.exports.globalData = res.extConfig;
          let s_app_id = wx.getStorageSync('whlw_app_id');
          if (s_app_id != module.exports.globalData.app_id) {
            that.setData({
              auth_modal: true
            })
          }
        }
      });
      this.triggerEvent('myevent', {})
    },
    // 取消授权
    cancel_auth: function () {
      let that = this;
      wx.showModal({
        title: '提示',
        content: '取消授权将影响使用, 确定要取消授权吗',
        success: function(res) {
          if (res.confirm === true) {
            that.setData({
              auth_modal: false
            })
          }
        }
      })
    },
    /**
     * 确认授权
     */
    bindGetUserInfo: function (e) {
      let userInfo = e.detail,
        that = this;
      this.setData({ userInfo: userInfo });
      if (e.detail.userInfo) {
        wx.showToast({
          title: '授权成功',
        })
        setTimeout(function () {
          let auth_rs = app.wlogin(userInfo);
          let employId = wx.getStorageSync('employId');
          if (!employId || employId == '') {
            app.employIdCallback = employId => {
              if (employId != '') {
                that.setData({
                  auth_modal: false
                })
                // wh.Post('/api/shop.Distribution/be_distributor', { pid: that.data.parent_id }, function(res) {
                //   console.log(res)
                // })
                wx.switchTab({
                  url: '/pages/index/index',
                  success: function (e) {
                    var page = getCurrentPages().pop();
                    if (page == undefined || page == null) return;
                    setTimeout(function() {
                      page.onShow()
                    }, 500)
                  }
                })
              }
            }
          } else {
            that.setData({
              auth_modal: false
            })
            wx.switchTab({
              url: '/pages/index/index',
              success: function (e) {
                var page = getCurrentPages().pop();
                if (page == undefined || page == null) return;
                page.onShow();
              }
            })
          }
        }, 1500)
      } else {
        that.cancel_auth()
      }
    }
  }
})
